package com.webex.ta.hydra.gui.wm;

import com.webex.ta.hydra.util.Comparing;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-10-20
 * Time: 8:37:46
 */
public class WindowInfo implements Cloneable {
    public static final float DEFAULT_VERTICAL_WEIGHT = 0.30f;
    public static final float DEFAULT_HORIZONTAL_WEIGHT = 0.20f;
    private boolean active;
    private int anchor;
    private boolean autoHide;

    private Rectangle floatingBounds;
    private String id;
    private String title;
    private ToolWindowType type;
    private boolean visible;
    private float weight;

    public WindowInfo(final String id) {
        active = false;
        anchor = SwingConstants.LEFT;
        autoHide = false;
        floatingBounds = null;
        this.id = id;
        title = "";
        type = ToolWindowType.DOCKED;
        visible = false;
        weight = DEFAULT_HORIZONTAL_WEIGHT;
    }

    public WindowInfo copy() {
        WindowInfo cp = null;
        try {
            cp = (WindowInfo) clone();
            if (floatingBounds != null) {
                cp.floatingBounds = (Rectangle) floatingBounds.clone();
            }
        } catch (CloneNotSupportedException ignore) {
            ignore.printStackTrace();
        }
        return cp;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof WindowInfo)) {
            return false;
        }
        final WindowInfo info = (WindowInfo) obj;
        return active == info.active &&
                anchor == info.anchor &&
                id.equals(info.id) &&
                autoHide == info.autoHide &&
                Comparing.equal(floatingBounds, info.floatingBounds) &&
                type == info.type &&
                visible == info.visible;
    }


    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public int getAnchor() {
        return anchor;
    }

    public void setAnchor(int anchor) {
        boolean isDefaultWeight;
        if (this.anchor == SwingConstants.BOTTOM || this.anchor == SwingConstants.TOP) {
            isDefaultWeight = weight == DEFAULT_VERTICAL_WEIGHT;
        } else {
            isDefaultWeight = weight == DEFAULT_HORIZONTAL_WEIGHT;
        }
        
        this.anchor = anchor;
        if (isDefaultWeight) {
            if (this.anchor == SwingConstants.BOTTOM || this.anchor == SwingConstants.TOP) {
                weight = DEFAULT_VERTICAL_WEIGHT;
            } else {
                weight = DEFAULT_HORIZONTAL_WEIGHT;
            }
        }
    }

    public boolean isAutoHide() {
        return autoHide;
    }

    public void setAutoHide(boolean autoHide) {
        this.autoHide = autoHide;
    }

    public Rectangle getFloatingBounds() {
        return floatingBounds;
    }

    public void setFloatingBounds(Rectangle floatingBounds) {
        this.floatingBounds = floatingBounds;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        if (title == null)
            return "";
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public ToolWindowType getType() {
        return type;
    }

    public void setType(ToolWindowType type) {
        this.type = type;
    }

    public boolean isFloating() {
        return type == ToolWindowType.FLOATING;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }
}
